var app = getApp();
Page({
  data: {
    zheng: "",
    fan: "",
    zheng64: "", //正面照base64
    fan64: "", //反面照base64
    img_z: "", //正面照后缀名
    img_f: "", //反面照后缀名
    name_z: "front_img", //正面照字段
    name_f: "reverse_img", //反面照字段
  },
  onLoad: function(e) {
    var that = this;
    var openid = wx.getStorageSync('openid');
    wx.request({
      url: 'https://www.mamingwang.cn/api/index/shi',
      data: {
        openid: openid
      },
      method: "post",
      success(res) {
        that.setData({
          message: res.data.data,
          e:e,
          zid: e.zid ? e.zid : ""
        })
      }
    })
  },
  //选择身份证正面照
  chooseImg: function() {
    var that = this;
    wx.chooseImage({
      count: 1,
      success(res) {
        var arr = res.tempFilePaths[0].split(".");
        that.setData({
          zheng: res.tempFilePaths[0],
          img_z: arr[arr.length - 1]
        })
        //图片转为base64
        wx.getFileSystemManager().readFile({
          filePath: res.tempFilePaths[0],
          encoding: "base64",
          success(res) {
            that.setData({
              zheng64: res.data
            })
          }
        })
      }
    })
  },
  //选择身份证反面照
  chooseImgfan: function() {
    var that = this;
    wx.chooseImage({
      count: 1,
      success(res) {
        var arr = res.tempFilePaths[0].split(".");
        that.setData({
          fan: res.tempFilePaths[0],
          img_f: arr[arr.length - 1]
        })
        //图片转为base64
        wx.getFileSystemManager().readFile({
          filePath: res.tempFilePaths[0],
          encoding: "base64",
          success(res) {
            that.setData({
              fan64: res.data
            })
          }
        })
      }
    })
  },
  //支付
  payMoney: function(e) {
    var openid = wx.getStorageSync("openid");
    var that = this;
    if (e.detail.value.name && e.detail.value.card && that.data.img_z && that.data.img_f) {
      wx.request({
        url: 'https://www.mamingwang.cn/api/paywx/pay',
        data: {
          openid: openid,
          price: this.data.e.price,
          flg: 1,
          tid: this.data.e.tid
        },
        method: "post",
        success(res) {
          console.log(res);
          //调起支付
          wx.requestPayment({
            'timeStamp': res.data.timeStamp,
            'nonceStr': res.data.nonceStr,
            'package': res.data.package,
            'signType': res.data.signType,
            'paySign': res.data.paySign,
            success: function(res) {
              wx.request({
                url: 'https://www.mamingwang.cn/api/index/shiming',
                data: {
                  openid: openid,
                  uname: e.detail.value.name, //姓名
                  card: e.detail.value.card, //身份证号
                  img_z: that.data.img_z, //正面照
                  img_f: that.data.img_f, //反面照
                  name_z: that.data.name_z, //正面照字段
                  name_f: that.data.name_f, //反面照字段
                  image_z: that.data.zheng64, //正面照base64
                  image_f: that.data.fan64 //反面照base64
                },
                method: "post",
                success(res) {
                  console.log(res)
                  wx.redirectTo({
                    url: 'renZhengIng/renZhengIng?zid=' + that.data.zid,
                  })
                }
              })
            },
          })
        }
      })
    } else {
      wx.showToast({
        title: '个人信息与身份证照片不能为空',
        icon: "none",
        duration: 2000,
        mask:true
      })
    }
  }

})